/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Compras;


import Controlador.Bean.Compras.Factura;
import Controlador.Bean.Compras.LineaOC;
import Controlador.Bean.Compras.ProductoxOrden;
import Controlador.Bean.Compras.ProductoxProveedor;
import Controlador.Bean.Compras.Proveedor;
import Controlador.Bean.Compras.ProvxProd;
import Controlador.Bean.Compras.Solicitud;
import Controlador.Bean.RRHH.Personal;
import Controlador.Service.Compras.GestionCompra;
import gui.General.Main;
import gui.Reportes.Produccion.Reportes;

//import gui.Almacen.nuevoProducto;
import java.awt.Component;


import java.util.ArrayList;

import java.util.Date;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;
import lib.Validaciones;

    /**
 *
 * @author LiziTa
 */
public class AgregarOrdenCompra extends javax.swing.JInternalFrame {

    /**
     * Creates new form AgregarOrdenCompra
     */
    public Solicitud s;
    public List<Proveedor> proveedor;
    public List<ProductoxOrden> prodxorden;
    public List<ProvxProd> productos;
   public List<Personal> empleado;
    private TableModel tb;
    int clic=0;
    String selectedId;
    gui.Compras.gestionCompras gestCompras;
    Integer linea=1; 
    Integer guardar;
    public String setid;
    public Integer cant=0;
    public Double pu=0.0;
   public Long numsol;
   public Integer setnum;
   public Double sub;
   public Double Sumita=0.0;
   public String setcmbprv;
   public String identif;
     public   ProductoxOrden p;
    public  Double precio=0.0;
   public int agrega=0;
   public int desbloquea=0;
    ArrayList<ProductoxOrden> lineasorden = new ArrayList<>();
      public List<ProductoxProveedor> pxprod;  
      public String codigo;
     //ESTADOS DE ORDEN DE COMPRA
     // 0 = CREADO
     // 1 = ELIMINADO
     // 2 = INGRESADO A ALMACEN
     public       String idprov="";
     ArrayList<LineaOC> lineas = new ArrayList<>();
    public AgregarOrdenCompra(gui.Compras.gestionCompras gc) {
        gestCompras = gc;
        initComponents();
        initComp();
    }

    AgregarOrdenCompra() {
         initComponents();
        initComp();
        
//          TableRowSorter<TableModel> sorter  = new TableRowSorter<TableModel>((TableModel) tblLineaOC.getModel());
//        tblLineaOC.setRowSorter(sorter);
        
       
         }

     private static AgregarOrdenCompra myoc=null;
     public static AgregarOrdenCompra getInstance() {
                 
          if(myoc == null){
                       myoc = new AgregarOrdenCompra();    
            Main.getDesktop().add(myoc); 
            myoc.setVisible(true);
               myoc.toFront();           
                    }
                    else{
                        if (myoc.isClosed()|| !myoc.isVisible()) {
                                myoc = new AgregarOrdenCompra(); 
                                Main.getDesktop().add(myoc);
                                myoc.toFront();
                                myoc.setVisible(true);
                        }
                        else
                            myoc.toFront();
                    }
        
        
        
                return myoc;
    }
    
    @SuppressWarnings("unchecked")
    private void  initComp(){
        
        
        
        tb = new TableModel();   
        tblLineaOC.setModel(tb);
        tblLineaOC.setRowSelectionAllowed(true);
        
        txtId.setEditable(false);
        fFecha.setDate(new Date());
        fFecha.setEditable(false);
        txtMonto.setEditable(false);
        txtMonto.setText("0.0");
        txtSubtotal.setText("0.0");
        txtPu.setText("0.0");
        txtCantidad.setText("0");
  
        txtSubtotal.setEditable(false);
        numsol= Controlador.Service.Compras.GestionCompra.verNumSol();

        proveedor =Controlador.Service.Compras.GestionCompra.obtenerProveedor();
        productos=Controlador.Service.Compras.GestionCompra.seleccionarProvxProd(idprov);
        empleado=Controlador.Service.Compras.GestionCompra.obtenerPersonal();
   
        cmbEmpleado.removeAllItems();
           
             cmbEmpleado.addItem(Main.usu.getNombUsuario());
        
            if (desbloquea==0){
            btnProducto.setEnabled(false);
            }
            else  btnProducto.setEnabled(true);
         
         tblLineaOC.setDefaultRenderer(JButton.class, new MiRender());
         tblLineaOC.setDefaultEditor( JButton.class, new MiEditor());
   txtF.setVisible(false);
  
         //"Id", "IdProducto","Producto","idproveedor","razon social","Precio","Cantidad","Subtotal","Fecha","Empleado","Eliminar"
    tblLineaOC.getColumnModel().getColumn(0).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(0).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(0).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(8).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(8).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(8).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(9).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(9).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(9).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(1).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(1).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(1).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(3).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(3).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(3).setPreferredWidth(0);
   
           
    }
    
    String id;
    String nombre;
 public Integer seagregalinea=1;
   
class TableModel extends AbstractTableModel{
 
        String[] columnNames = {"Id", "IdProducto","Producto","idproveedor","razon social","Precio","Cantidad","Subtotal","Fecha","Empleado","Eliminar"};
        
                
         public void actualizarTabla(){
//           data1 = Controlador.Service.Compras.GestionCompra.obtenerProdxOrden(identif);
               tblLineaOC.getColumnModel().getColumn(0).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(0).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(0).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(8).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(8).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(8).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(9).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(9).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(9).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(1).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(1).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(1).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(3).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(3).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(3).setPreferredWidth(0);
             tb.fireTableDataChanged();
               tblLineaOC.getColumnModel().getColumn(0).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(0).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(0).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(8).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(8).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(8).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(9).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(9).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(9).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(1).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(1).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(1).setPreferredWidth(0);
              tblLineaOC.getColumnModel().getColumn(3).setMaxWidth(0);
           tblLineaOC.getColumnModel().getColumn(3).setMinWidth(0);
             tblLineaOC.getColumnModel().getColumn(3).setPreferredWidth(0);
           
        
        }      
         
          public void actualizarTabla2(){
//           data1 = Controlador.Service.Compras.GestionCompra.obtenerProdxOrden("");
//           fireTableDataChanged();
           
        } 
         
        
//        public void actualizarTabla2(){
//            data = Controlador.Service.Compras.GestionCompra.filtrarSolicitudes(id, nombre);
//            fireTableDataChanged();
//         id="";
//            nombre="";
//        }
        
        @Override
        public int getRowCount()
        {
            return lineas.size();
        }
        
        
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   
            
            String res=null;
             LineaOC Loc=lineas.get(row);
            switch(column){
               
                case 0: res = Loc.getIdordencompra()+""; break;
                case 1: res = Loc.getIdProducto()+""; break;
                case 2: res = Loc.getNomProd()+""; break;
                case 3: res = Loc.getIdProveedor()+""; break;
                case 4: res = Loc.getNomProveedor()+""; break;
                case 5: res = Loc.getPrecio()+""; break;
                case 6:res = Loc.getCantidad()+""; break;
                case 7: res = Loc.getSubtotal()+""; break;
                case 8: res = Loc.getFecha()+""; break;
                case 9: res = Loc.getIdempleado() +""; break;   
                 
                default: return new JButton("Eliminar");
            }
            return res;
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
           
      if(c!=10){
            return getValueAt(0, c).getClass();
            }else{
                return JButton.class;
            }
          
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
	    if (column==6) return true; else     
            
            return false;
        }
        
        
         @Override
        public void setValueAt(Object value,  int row, int col){
            LineaOC liOC = lineas.get(row);
            if(col==6){
                
                String c=value.toString();
                
                if ("0".equals(c))JOptionPane.showMessageDialog(null, "Cantidad no puede ser cero" );
                    else {
                
                if (Validaciones.validarEntero(c)){
                                        
                Integer Cant = Integer.parseInt(value.toString());
                
                liOC.setCantidad(Cant);
                Double res=Cant.doubleValue()*liOC.getPrecio();
               
                
                //actualizar subtotal y monto total
                lineas.get(row).setSubtotal(res);
             
                 double montoSoles = 0.0;
        for(int i=0;i<lineas.size();i++){
            montoSoles=montoSoles+lineas.get(i).getSubtotal();
        }
        txtMonto.setText(String.valueOf(montoSoles));
        tb.actualizarTabla();  } 

 
                  
                    
                else  JOptionPane.showMessageDialog(null, "Datos no validos para cantidad" );
            }
             } 
                    
        }
        
        
     }
     
     public class MiRender implements TableCellRenderer {
        // Este metodo indica como debe de pintarse el elemento en la fila row, en la columna column
        // que esta en la tabla table y tiene el color dado por el objeto.
        @Override
        public Component getTableCellRendererComponent(JTable table, Object color, boolean isSelected, boolean hasFocus, int row, int column) {    
            // como el boton y el calendario son componentes esto se vale.
            return (JComponent)table.getValueAt(row, column);
        }
     }
     
     public class MiEditor extends AbstractCellEditor implements TableCellEditor {
         private Boolean currentValue;
         @Override
         public Object getCellEditorValue()
         {
            return currentValue;
         }
         //El editor usara el propio componente. Para que funcione la celda en el modelo debe ser editable.
         @Override
         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
         {
           return (JComponent) value;// la tabla solo debe tener componentes graficos
         }
     }
    
/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
     
     
     //VALIDACIONES
     public boolean valido(){
     
         boolean b = true;
         boolean cantidad = Validaciones.validarEntero(txtCantidad.getText());
         boolean vpu=Validaciones.validarReal(txtPu.getText());
         boolean stotal=Validaciones.validarReal(txtSubtotal.getText());
         String s1="";
         String s2="";
         String s3="";
         String s4="";
         String s5="";
         if(txtIdProducto.getText().isEmpty())
             {
            b = false;
           s1="Seleccionar Producto";
        }
          if(txtIdProveedor.getText().isEmpty())
             {
            b = false;
           s1="Seleccionar Proveedor";
        }
          
          if( "0".equals(txtCantidad.getText())){
            b = false;
             s5="La cantidad de producto no debe ser cero";
        }
        
          if(txtCantidad.getText().isEmpty() || !cantidad ){
            b = false;
           s2="Ingresar cantidad del producto";
        }
          
          if( "0.0".equals(txtPu.getText())){
            b = false;
            
        }
           if( "0.0".equals(txtSubtotal.getText())){
            b = false;
            
        }
          
          if(txtPu.getText().isEmpty() || !vpu){
            b = false;
            s3="Error en Precio Unitario";
        }
          
           if(txtSubtotal.getText().isEmpty() || !stotal ){
            b = false;
            s4= "Error en SubTotal";
        }
           
           String rpta="";
         if (s1!="") rpta = rpta+ s1 + "\n";
         if (s2!="") rpta = rpta+ s2 + "\n";
        if (s3!="") rpta = rpta+ s3 + "\n";
         if (s4!="") rpta = rpta+ s4 + "\n";
          if (s5!="") rpta = rpta+ s5 + "\n";
                
         if (rpta!="")
         JOptionPane.showMessageDialog(null, "Errores: \n" + rpta );
          
         return b;
     }
       
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cmbEmpleado = new javax.swing.JComboBox();
        fFecha = new org.jdesktop.swingx.JXDatePicker();
        btnProveedor = new javax.swing.JButton();
        txtIdProveedor = new javax.swing.JTextField();
        txtNomProveedor = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtNomProducto = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtPu = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtSubtotal = new javax.swing.JTextField();
        txtF = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnProducto = new javax.swing.JButton();
        txtIdProducto = new javax.swing.JTextField();
        txtNomProd = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        btnEnviar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLineaOC = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        txtMonto = new javax.swing.JTextField();
        btnGuardar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Orden de Compra");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos generales"));

        jLabel1.setText("IdOrden:");

        txtId.setEditable(false);
        txtId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIdActionPerformed(evt);
            }
        });

        jLabel2.setText("Proveedor:");

        jLabel4.setText("Empleado:");

        jLabel6.setText("Fecha:");

        cmbEmpleado.setEnabled(false);

        btnProveedor.setText("Seleccionar Proveedor");
        btnProveedor.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnProveedorMouseClicked(evt);
            }
        });
        btnProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProveedorActionPerformed(evt);
            }
        });

        txtIdProveedor.setEditable(false);
        txtIdProveedor.setEnabled(false);

        txtNomProveedor.setEditable(false);
        txtNomProveedor.setEnabled(false);
        txtNomProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomProveedorActionPerformed(evt);
            }
        });

        jLabel5.setText("IdProveedor:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(fFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnProveedor)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(txtNomProveedor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                        .addComponent(txtIdProveedor, javax.swing.GroupLayout.Alignment.LEADING)))
                .addGap(40, 40, 40))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtIdProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNomProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnProveedor))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtNomProducto.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles"));

        jLabel7.setText("Precio Unitario:");

        jLabel8.setText("Cantidad:");

        txtPu.setEditable(false);
        txtPu.setAutoscrolls(false);
        txtPu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                txtPuMouseExited(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                txtPuMouseReleased(evt);
            }
        });
        txtPu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPuActionPerformed(evt);
            }
        });
        txtPu.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPuKeyReleased(evt);
            }
        });

        txtCantidad.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                txtCantidadMouseExited(evt);
            }
        });
        txtCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCantidadActionPerformed(evt);
            }
        });
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCantidadKeyReleased(evt);
            }
        });

        jLabel10.setText("Subtotal:");

        txtSubtotal.setEditable(false);
        txtSubtotal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubtotalActionPerformed(evt);
            }
        });

        txtF.setEditable(false);
        txtF.setEnabled(false);

        jLabel3.setText("Producto:");

        btnProducto.setText("Seleccionar Producto");
        btnProducto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnProductoActionPerformed(evt);
            }
        });

        txtIdProducto.setEditable(false);
        txtIdProducto.setEnabled(false);

        txtNomProd.setEditable(false);
        txtNomProd.setEnabled(false);
        txtNomProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomProdActionPerformed(evt);
            }
        });

        jLabel11.setText("IdProducto:");

        javax.swing.GroupLayout txtNomProductoLayout = new javax.swing.GroupLayout(txtNomProducto);
        txtNomProducto.setLayout(txtNomProductoLayout);
        txtNomProductoLayout.setHorizontalGroup(
            txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(txtNomProductoLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(txtNomProductoLayout.createSequentialGroup()
                        .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtNomProd, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnProducto))
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(txtNomProductoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addGap(10, 10, 10)
                        .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPu, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(txtNomProductoLayout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(txtF, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(48, 48, 48))
        );
        txtNomProductoLayout.setVerticalGroup(
            txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(txtNomProductoLayout.createSequentialGroup()
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnProducto)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(txtNomProductoLayout.createSequentialGroup()
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(txtNomProductoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(txtNomProductoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, txtNomProductoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))))
        );

        btnEnviar.setText("Agregar fila");
        btnEnviar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEnviarMouseClicked(evt);
            }
        });
        btnEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblLineaOC.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        tblLineaOC.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7", "Title 8", "Title 9", "Title 10", "Title 11"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, true, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblLineaOC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblLineaOCMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblLineaOC);

        jLabel9.setText("Monto total:");

        txtMonto.setEditable(false);
        txtMonto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                txtMontoMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                txtMontoMouseExited(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                txtMontoMouseReleased(evt);
            }
        });
        txtMonto.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                txtMontoComponentAdded(evt);
            }
        });
        txtMonto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMontoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtMonto, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(txtMonto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        btnGuardar.setText("Guardar ");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        jButton2.setText("Descartar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(70, 70, 70))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNomProducto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnEnviar)
                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNomProducto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEnviar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(btnGuardar))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

     
    public Integer num=0;
    
    public void limpiardetalle(){
        
        btnProducto.setEnabled(true);
       txtCantidad.setText("0");
txtIdProducto.setText("");
txtNomProd.setText("");
    txtPu.setText("0.0");
    txtPu.setEnabled(false);
    txtSubtotal.setText("0.0");
       
    }
    
    
    private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviarActionPerformed
        // TODO add your handling code here:
        
     
        agrega=1;
       if (valido()){ 
         
String txtprod;
String txtProv;
Integer flag=0;
            for (int i=0; i<lineas.size(); i++){
                String prod=lineas.get(i).getIdProducto();
                String prov=lineas.get(i).getIdProveedor();
                txtprod=txtIdProducto.getText();
                txtProv=txtIdProveedor.getText();
                
                if (txtprod.equals(prod) && txtProv.equals(prov)){
                     JOptionPane.showMessageDialog(this, "No se puede ingresar productos repetidos", "Error", JOptionPane.OK_OPTION);
                      flag=1;
                }else flag=0;
                
      
      }
          if (flag==0){ 
           seagregalinea=0;
               String iorden=Controlador.Service.Compras.GestionCompra.obtenerIdSiguienteOC();
                txtId.setText(iorden);
       LineaOC lin = new LineaOC(txtIdProducto.getText(),
                 txtIdProveedor.getText(),txtNomProd.getText(), txtNomProveedor.getText(),
                 Double.valueOf(txtPu.getText()), Integer.parseInt(txtCantidad.getText()), Double.valueOf(txtSubtotal.getText()),  Double.parseDouble(txtMonto.getText()),
       txtId.getText(),new Date(),Main.usu.getIdEmpleadoAdm());
       lineas.add(lin);
       
         
         
          
          Double montof=0.0;
         for (int i=0; i<lineas.size(); i++){
             montof=montof+lineas.get(i).getSubtotal();
         
         }
          txtMonto.setText(montof.toString());
         
           tb.actualizarTabla();
           limpiardetalle();
 }
      }
    }//GEN-LAST:event_btnEnviarActionPerformed

     public double Redondear(double numero){
        return Math.rint(numero*100)/100;
    }
    
    
    private void txtCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCantidadActionPerformed
        // TODO add your handling code here:
        if (txtCantidad.getText().isEmpty()|| !Validaciones.validarEntero(txtCantidad.getText())) cant=0;
         else 
        cant=Integer.parseInt(txtCantidad.getText());
         
         Double Total=cant*pu;
         Double t=Redondear(Total);
        txtSubtotal.setText(t.toString());
    }//GEN-LAST:event_txtCantidadActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
if (seagregalinea==0 && !lineas.isEmpty()){       
        
        guardar=1;
        txtId.setText("");

         Date fecha= new Date();
        Double montof=0.0;
         for (int i=0; i<lineas.size(); i++){
             montof=montof+lineas.get(i).getSubtotal();
         
         }
         String iorden=Controlador.Service.Compras.GestionCompra.obtenerIdSiguienteOC();
         
      Integer filas= lineas.size();
       s= new Solicitud(iorden,txtIdProveedor.getText(),Main.usu.getIdEmpleadoAdm(),
       0,fecha,montof,"0");
       
       GestionCompra.agregarSolicitud(s);
      
      
       
      for (int i=0; i<filas; i++){
       p = new ProductoxOrden(iorden,lineas.get(i).getIdProducto(),i+1,lineas.get(i).getPrecio(),lineas.get(i).getCantidad(),lineas.get(i).getSubtotal());
          GestionCompra.agregarProductoxOrden(p,iorden);
      }
      
         tb.actualizarTabla();
          JOptionPane.showMessageDialog(null,"Se generó Orden de Compra N° "+p.getIdOrdenCompra());
                                               
           tb.actualizarTabla(); 
          int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea visualizar Orden de Compra?");
            if (confirmado == JOptionPane.OK_OPTION){
            Reportes r=new Reportes();
            r.mostrarReporteOrdenCompraUnica(p.getIdOrdenCompra());
            
            }
           
           //Generar factura
           
             int confirmado2 = JOptionPane.showConfirmDialog(this,"¿Desea generar una factura?");
            if (confirmado2 == JOptionPane.OK_OPTION){
                        
       Factura f= new Factura("",fecha,s.getTotal(),"Generada de orden de compra","ACTIVO",s.getIdOrdenCompra());
     
       GestionCompra.agregarFactura(f); 
         JOptionPane.showMessageDialog(null,"Se generó la factura N°"+f.getIdfacturacompra()+" con éxito ");
          tb.actualizarTabla2(); 
            }else {
                                
             limpiar();
             tb.actualizarTabla2();
            }
        
        tb.actualizarTabla2(); 
         txtId.setText("");
    txtMonto.setText("");
    txtCantidad.setText("0");
//  txtNumSol.setText("");
    txtPu.setText("0.0");
    txtPu.setEnabled(false);
    txtSubtotal.setText("");
    fFecha.setDate(new Date());
    
       
           limpiar();
         
         txtF.setText("");} 

else   JOptionPane.showMessageDialog(this, "No se registró ningun producto", "Error", JOptionPane.OK_OPTION);
        // TODO add your handling code here:
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtMontoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMontoActionPerformed
        // TODO add your handling 
        
       
       
    }//GEN-LAST:event_txtMontoActionPerformed

    private void txtPuKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPuKeyReleased

        // TODO add your handling code here:
    
    }//GEN-LAST:event_txtPuKeyReleased

    private void txtCantidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyReleased
   
                
        
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCantidadKeyReleased

    private void txtPuMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPuMouseReleased
  
   // TODO add your handling code here:
    }//GEN-LAST:event_txtPuMouseReleased

    private void txtPuMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtPuMouseExited
   
   // TODO add your handling code here:
    }//GEN-LAST:event_txtPuMouseExited

    private void txtCantidadMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtCantidadMouseExited
        String can="0";
         String pru="0";
        if (txtCantidad.getText().isEmpty()|| !Validaciones.validarEntero(txtCantidad.getText())){
        can="0";
        }
        else can=txtCantidad.getText();
        if (txtPu.getText().isEmpty()|| !Validaciones.validarEntero(txtCantidad.getText())){
        pru="0";
        }
        else pru=txtPu.getText();
        
        
        Double pre=Double.parseDouble(pru);
         Double ca=Double.parseDouble(can);
         Double mont=pre*ca;
                  Double t=Redondear(mont);
        txtSubtotal.setText(t.toString());
        
        
         
    }//GEN-LAST:event_txtCantidadMouseExited

    private void txtMontoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMontoMouseExited
        
           
    }//GEN-LAST:event_txtMontoMouseExited

    private void txtMontoComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_txtMontoComponentAdded
       // TODO add your handling code here:
    }//GEN-LAST:event_txtMontoComponentAdded

    private void txtMontoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMontoMouseEntered
 
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMontoMouseEntered

    private void txtMontoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMontoMouseReleased
       // TODO add your handling code here:
    }//GEN-LAST:event_txtMontoMouseReleased

    private void tblLineaOCMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblLineaOCMouseClicked
        // TODO add your handling code here:
              int selectedRowIndex = tblLineaOC.getSelectedRow();
        int selectedColumnIndex = tblLineaOC.getSelectedColumn();
        txtF.setText("F");
        
        if(selectedColumnIndex!=10){
            String selectId = (String) tblLineaOC.getValueAt(selectedRowIndex, 0);
          
            if(clic == 1 && selectedId.compareTo(selectId)==0){
               txtId.setText((String)tblLineaOC.getValueAt(selectedRowIndex, 0));
                txtIdProducto.setText((String)tblLineaOC.getValueAt(selectedRowIndex, 1));
               txtNomProd.setText((String)tblLineaOC.getValueAt(selectedRowIndex, 2));
                txtIdProveedor.setText((String)tblLineaOC.getValueAt(selectedRowIndex, 3));
                txtNomProveedor.setText((String)tblLineaOC.getValueAt(selectedRowIndex, 4));
                txtPu.setText(((String)tblLineaOC.getValueAt(selectedRowIndex, 5)));
                txtCantidad.setText(((String)tblLineaOC.getValueAt(selectedRowIndex, 6)));
                txtSubtotal.setText(((String)tblLineaOC.getValueAt(selectedRowIndex, 7)));
              
                clic = 0;
             
            }else if(clic == 0){
                selectedId = selectId;
                clic++;
            }else{
                clic = 0;
            }
        }else{
            int confirmado = JOptionPane.showConfirmDialog(this,"¿Desea eliminar el producto agregado?");
            if (confirmado == JOptionPane.OK_OPTION){
                                    
                           
                     lineas.remove(selectedRowIndex);
                     limpiardetalle();
                     double montoSoles = 0.0;
        for(int i=0;i<lineas.size();i++){
            montoSoles=montoSoles+lineas.get(i).getSubtotal();
        }
        txtMonto.setText(String.valueOf(montoSoles));
            tb.actualizarTabla();
            }
            
            
            
        }
        
        
    }//GEN-LAST:event_tblLineaOCMouseClicked

    public void limpiar(){
    
    
      
       btnProveedor.setEnabled(true);
        btnProducto.setEnabled(false);
       lineas=new ArrayList<>();
       txtMonto.setText("");
    txtCantidad.setText("0");
txtIdProducto.setText("");
txtNomProd.setText("");
txtIdProveedor.setText("");
txtNomProveedor.setText("");
    txtPu.setText("0.0");
    txtPu.setEnabled(false);
    txtSubtotal.setText("");
    fFecha.setDate(new Date());
    txtId.setText("");  
    tb.actualizarTabla();
    initComp();
    
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

       txtF.setText("");
      limpiar();
       
       
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void cmbEmpleadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbEmpleadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cmbEmpleadoActionPerformed

    private void txtSubtotalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubtotalActionPerformed
        // TODO add your handling code here:
     
    }//GEN-LAST:event_txtSubtotalActionPerformed

     
    
    private void cmbProductoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmbProductoMouseExited
        // TODO add your handling code here:
   
    }//GEN-LAST:event_cmbProductoMouseExited

    private void cmbProductoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmbProductoMouseClicked
        // TODO add your handling code here:
     
    }//GEN-LAST:event_cmbProductoMouseClicked

    private void cmbProductoMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmbProductoMouseReleased
        // TODO add your handling code here:
       
    }//GEN-LAST:event_cmbProductoMouseReleased

    private void txtPuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPuActionPerformed
        // TODO add your handling code here:
         if (txtPu.getText().isEmpty()) pu=0.0;
    else 
        pu=Double.parseDouble(txtPu.getText());
    Double Total=cant*pu;
     Double t=Redondear(Total);
        txtSubtotal.setText(t.toString());
 
    
    }//GEN-LAST:event_txtPuActionPerformed

    private void txtIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIdActionPerformed

    private void btnProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProveedorActionPerformed
        // TODO add your handling code here:
        OCSeleccionarProveedor csp=new OCSeleccionarProveedor();
        Main.getDesktop().add(csp);
        csp.setVisible(true);
        
           if ( !txtIdProveedor.getText().isEmpty()){
        btnProducto.setEnabled(true);
        } else
             btnProducto.setEnabled(false); 
        
    }//GEN-LAST:event_btnProveedorActionPerformed

    private void txtNomProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomProveedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomProveedorActionPerformed

    private void txtNomProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomProdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomProdActionPerformed

    private void btnProveedorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnProveedorMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount()==1 && !txtIdProveedor.getText().isEmpty()){
        btnProducto.setEnabled(true);
        } else
             btnProducto.setEnabled(false);
            
    }//GEN-LAST:event_btnProveedorMouseClicked

    private void btnProductoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnProductoActionPerformed
        // TODO add your handling code here:
          codigo=txtIdProveedor.getText();
         
        OCSeleccionarProducto ocsp=new OCSeleccionarProducto(codigo);
        Main.getDesktop().add(ocsp);
        ocsp.setVisible(true);
        
    }//GEN-LAST:event_btnProductoActionPerformed

    private void btnEnviarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEnviarMouseClicked
        // TODO add your handling code here:
        
         if ( evt.getClickCount()==1){
             btnProveedor.setEnabled(false);
                     
         }       
    }//GEN-LAST:event_btnEnviarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEnviar;
    private javax.swing.JButton btnGuardar;
    public static javax.swing.JButton btnProducto;
    private javax.swing.JButton btnProveedor;
    private javax.swing.JComboBox cmbEmpleado;
    private org.jdesktop.swingx.JXDatePicker fFecha;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    public static javax.swing.JTable tblLineaOC;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtF;
    private javax.swing.JTextField txtId;
    public static javax.swing.JTextField txtIdProducto;
    public static javax.swing.JTextField txtIdProveedor;
    private javax.swing.JTextField txtMonto;
    public static javax.swing.JTextField txtNomProd;
    private javax.swing.JPanel txtNomProducto;
    public static javax.swing.JTextField txtNomProveedor;
    public static javax.swing.JTextField txtPu;
    private javax.swing.JTextField txtSubtotal;
    // End of variables declaration//GEN-END:variables
}
